package com.hk.demo.player.grid;

import android.view.View;

import com.zhoug.common.utils.LogUtils;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author: zhoug
 * @since: 2025-08-25
 */
public class RecyclerViewUtils {
    private static final String TAG = ">>>RecyclerViewUtils";

    /**
     * 找到所有已经存在的ViewHolder
     * @param recyclerView
     * @param consumer
     */
   public static <T extends RecyclerView.ViewHolder> void findViewHolder(RecyclerView recyclerView, @NonNull Consumer<T> consumer) {
        if (recyclerView != null) {
            int childCount = recyclerView.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = recyclerView.getChildAt(i);
                try {
                    RecyclerView.ViewHolder childViewHolder = recyclerView.getChildViewHolder(child);
                    if (childViewHolder!=null) {
                        consumer.accept((T) childViewHolder);
                    }
                } catch (Exception e) {
                    LogUtils.e(TAG, "findViewHolder:" + e.getMessage());
                }
            }
        }
    }

}
